package com.niuquan.uicomponent;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.niuquan.activitie.R;

import java.util.ArrayList;

/**
 * Created by lewen on 2015/5/23.
 */
public class starlist extends LinearLayout {
//
//    private ImageButton star1;
//    private ImageButton star2;
//    private ImageButton star3;
//    private ImageButton star4;
//    private ImageButton star5;

    ImageButton star1 = null;
    ImageButton star2 = null;
    ImageButton star3 = null;
    ImageButton star4 = null;
    ImageButton star5 = null;
    int count;
    ArrayList<ImageButton> starlist = null;
    int number = 1;
    public starlist(Context context,int number) {
        super(context);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.fragment_stars, this);
        starlist = new ArrayList<ImageButton>();
        starlist.add((ImageButton) findViewById(R.id.star1));
        starlist.add((ImageButton) findViewById(R.id.star2));
        starlist.add((ImageButton) findViewById(R.id.star3));
        starlist.add((ImageButton) findViewById(R.id.star4));
        starlist.add((ImageButton) findViewById(R.id.star5));
    }


    public starlist(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.fragment_stars, this);
        starlist = new ArrayList<ImageButton>();
        starlist.add((ImageButton) findViewById(R.id.star1));
        starlist.add((ImageButton) findViewById(R.id.star2));
        starlist.add((ImageButton) findViewById(R.id.star3));
        starlist.add((ImageButton) findViewById(R.id.star4));
        starlist.add((ImageButton) findViewById(R.id.star5));
    }
    public starlist(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.fragment_stars, this);
        starlist = new ArrayList<ImageButton>();
        starlist.add((ImageButton) findViewById(R.id.star1));
        starlist.add((ImageButton) findViewById(R.id.star2));
        starlist.add((ImageButton) findViewById(R.id.star3));
        starlist.add((ImageButton) findViewById(R.id.star4));
        starlist.add((ImageButton) findViewById(R.id.star5));


        star1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setstar_num(1);
                number = 1;
            }
        });
        star2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setstar_num(2);
                number = 2;
            }
        });
        star3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setstar_num(3);
                number = 3;
            }
        });
        star4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setstar_num(4);
                number = 4;
            }
        });
        star5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setstar_num(5);
                number = 5;
            }
        });
    }
    public void setstar_num(int num)
    {
        if(num!=0)
        {
            int i ;
            for(i = 0;i<5;i++)
            {
                ImageButton star = (ImageButton)starlist.get(i);
                if(i<num)
                {
                    star.setImageDrawable(getResources().getDrawable(R.drawable.img_star_light));
                }
                else {
                    star5.setImageDrawable(getResources().getDrawable(R.drawable.img_star_fade));
                }
            }
        }
    }

    public int getnumber()
    {
        return number;
    }
}
